package com.laiyima.zhongjiang.linghuilv.demo.home;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.laiyima.zhongjiang.linghuilv.demo.R;
import com.laiyima.zhongjiang.linghuilv.demo.util.FixedEditText;

import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import me.imid.swipebacklayout.lib.app.SwipeBackActivity;

public class NameActivity extends SwipeBackActivity implements View.OnClickListener {
    private FixedEditText InputName, InputId, InputBank, InputResnumber;
    private Button myButton;
    private String TAG = "NameActivity";
    private int realcode, HomeCode;
    private LinearLayout imageView;


    protected boolean useThemestatusBarColor = false;//是否使用特殊的标题栏背景颜色，android5.0以上可以设置状态栏背景色，如果不使用则使用透明色值

    protected void setStatusBar() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {//5.0及以上
            View decorView = getWindow().getDecorView();
            int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
            decorView.setSystemUiVisibility(option);
            //根据上面设置是否对状态栏单独设置颜色
            if (useThemestatusBarColor) {
                // getWindow().setStatusBarColor(getResources().getColor(R.color.colorTheme));
            } else {
                getWindow().setStatusBarColor(Color.TRANSPARENT);
            }
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {//4.4到5.0
            WindowManager.LayoutParams localLayoutParams = getWindow().getAttributes();
            localLayoutParams.flags = (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | localLayoutParams.flags);
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {//android6.0以后可以对状态栏文字颜色和图标进行修改
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_name);
        InputName = (FixedEditText) findViewById(R.id.inputName);
        InputName.setLeadText("姓名          ");
        InputId = (FixedEditText) findViewById(R.id.inputId);
        InputId.setLeadText("身份证号   ");
        InputBank = (FixedEditText) findViewById(R.id.inputBank);
        InputBank.setLeadText("银行卡       ");
        InputResnumber = (FixedEditText) findViewById(R.id.inputResnumber);
        InputResnumber.setLeadText("预留手机号");
        imageView = (LinearLayout) findViewById(R.id.name_back);
        imageView.setOnClickListener(this);
        myButton = (Button) findViewById(R.id.read_name_button);
        myButton.setOnClickListener(this);
        intView();
        setStatusBar();

    }

    private void intView() {
        SharedPreferences sharedPreferences =getSharedPreferences("userinfo.txt", Context.MODE_PRIVATE);
        String userID = sharedPreferences.getString("userID", "");
        RequestParams params = new RequestParams("http://zljl.laiima.com/index.php?s=/lfl_api/Commonlfl/query_real");
        //需要带入的参数
        params.addBodyParameter("uid", userID);
        // params.addBodyParameter("captcha", captcha);
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    HomeCode = jsonObject.optInt("code", 0);
                    String data = jsonObject.optString("data", null);
                    if (!TextUtils.isEmpty(data)) {
                        JSONObject jsonObjec = new JSONObject(data);
                        String name = jsonObjec.optString("name", "");
                        InputName.setText(name);
                        InputName.setEnabled(false);
                        InputName.setTextColor(Color.BLACK);
                        String identity = jsonObjec.optString("idCard", "");
                         InputId.setText(identity);
                         InputId.setEnabled(false);
                        InputId.setTextColor(Color.BLACK);
                        String back = jsonObjec.optString("bankCard", "");
                         InputBank.setText(back);
                        InputBank.setEnabled(false);
                        InputBank.setTextColor(Color.BLACK);
                        String mobile = jsonObjec.optString("mobile", "");
                        InputResnumber.setText(mobile);
                        InputResnumber.setEnabled(false);
                        InputResnumber.setTextColor(Color.BLACK);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                if (HomeCode == 200) {
                    // Toast.makeText(getBaseContext().getApplicationContext(), "你已实名认证", Toast.LENGTH_SHORT).show();
                    // myButton.setText("返回");
                    myButton.setText("你已实名认证");
                    myButton.setBackgroundResource(R.drawable.sginbutton);
                    myButton.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            finish();
                        }
                    });
                }
                if (HomeCode == 404) {
                 }
                if (HomeCode == 500) {
                 }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.e(TAG, "查看实名认证失败");
            }
            @Override
            public void onCancelled(CancelledException cex) {

            }
            @Override
            public void onFinished() {

            }
        });
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.name_back:
                finish();
                break;

            case R.id.read_name_button:
                String name = InputName.getText().toString();
                String identity = InputId.getText().toString();
                String back = InputBank.getText().toString();
                String mobile = InputResnumber.getText().toString();
                if (name.isEmpty()) {
                    Toast.makeText(getBaseContext().getApplicationContext(), "请输入用户名", Toast.LENGTH_SHORT).show();
                    return;
                } else if (identity.isEmpty()) {
                    Toast.makeText(getBaseContext().getApplicationContext(), "请输入身份证号", Toast.LENGTH_SHORT).show();
                    return;
                } else if (back.isEmpty()) {
                    Toast.makeText(getBaseContext().getApplicationContext(), "请输入银行卡号", Toast.LENGTH_SHORT).show();
                    return;
                } else if (mobile.isEmpty()) {
                    Toast.makeText(getBaseContext().getApplicationContext(), "请输入预留手机号", Toast.LENGTH_SHORT).show();
                    return;
                } else {
                    SharedPreferences sharedPreferences = getSharedPreferences("userinfo.txt", Context.MODE_PRIVATE);
                    String userID = sharedPreferences.getString("userID", "");
                    RequestParams params = new RequestParams("http://zljl.laiima.com/index.php?s=/lfl_api/Commonlfl/real");
                    //需要带入的参数
                    params.addBodyParameter("uid", userID);
                    params.addBodyParameter("name", name);
                    params.addBodyParameter("idCard", identity);
                    params.addBodyParameter("backCard", back);
                    params.addBodyParameter("mobile", mobile);
                    x.http().post(params, new Callback.CommonCallback<String>() {
                        @Override
                        public void onSuccess(String result) {

                            try {
                                JSONObject jsonObject = new JSONObject(result);
                                realcode = jsonObject.optInt("code", 0);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                            if (realcode == 200) {
                                Toast.makeText(getBaseContext().getApplicationContext(), "实名认证成功", Toast.LENGTH_SHORT).show();
                                finish();
                            }
                            if (realcode == 201) {
                                Toast.makeText(getBaseContext().getApplicationContext(), "实名认证失败", Toast.LENGTH_SHORT).show();
                            }
                            if (realcode == 501) {
                                Toast.makeText(getBaseContext().getApplicationContext(), "请核对信息是否完整", Toast.LENGTH_SHORT).show();
                            }
                            if (realcode == 402) {
                                Toast.makeText(getBaseContext().getApplicationContext(), "已实名，无需重复提交", Toast.LENGTH_SHORT).show();
                            }
                            if (realcode == 402) {
                                Toast.makeText(getBaseContext().getApplicationContext(), "已实名，无需重复提交", Toast.LENGTH_SHORT).show();
                            }
                            if (realcode == 500) {
                                Toast.makeText(getBaseContext().getApplicationContext(), "系统异常", Toast.LENGTH_SHORT).show();
                            } else {
                                // Toast.makeText(getBaseContext().getApplicationContext(), "信息核实不正确", Toast.LENGTH_SHORT).show();
                            }
                        }

                        @Override
                        public void onError(Throwable ex, boolean isOnCallback) {
                        }

                        @Override
                        public void onCancelled(CancelledException cex) {

                        }

                        @Override
                        public void onFinished() {

                        }
                    });

                }
                break;
        }

    }
}
